package io.renren.system.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.system.sys.dao.SysDictDataDao;
import io.renren.system.sys.dto.SysDictDataDTO;
import io.renren.system.sys.entity.SysDictDataEntity;
import io.renren.system.sys.service.SysDictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 系统设置-字典管理-字典数据
 */
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataDao, SysDictDataEntity> implements SysDictDataService {

    private QueryWrapper<SysDictDataEntity> getWrapper(Map<String, Object> params) {
        String dictTypeId = (String) params.get("dictTypeId");
        String dictLabel = (String) params.get("dictLabel");
        String dictValue = (String) params.get("dictValue");

        QueryWrapper<SysDictDataEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type_id", dictTypeId);
        wrapper.like(StringUtils.isNotBlank(dictLabel), "dict_label", dictLabel);
        wrapper.like(StringUtils.isNotBlank(dictValue), "dict_value", dictValue);
        return wrapper;
    }

    /**
     * 根据查询条件分页获取字典数据列表
     *
     * @param params
     * @return
     */
    @Override
    public PageData<SysDictDataDTO> page(Map<String, Object> params) {
        IPage<SysDictDataEntity> page = baseDao.selectPage(
                getPage(params, "sort", true),
                getWrapper(params)
        );
        return getPageData(page, SysDictDataDTO.class);
    }

    /**
     * 根据主键获取字典数据详细信息
     *
     * @param id
     * @return
     */
    @Override
    public SysDictDataDTO get(Long id) {
        SysDictDataEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, SysDictDataDTO.class);
    }

    /**
     * 新增保存字典数据信息
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictDataDTO dto) {
        SysDictDataEntity entity = ConvertUtils.sourceToTarget(dto, SysDictDataEntity.class);
        insert(entity);
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictDataDTO dto) {
        SysDictDataEntity entity = ConvertUtils.sourceToTarget(dto, SysDictDataEntity.class);
        updateById(entity);
    }

    /**
     * 根据主键删除字典数据信息
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据字典类型和字典值获取字典数据详细信息
     *
     * @param dictTypeId
     * @param dictValue
     * @return
     */
    @Override
    public SysDictDataEntity selectByDictTypeId(Long dictTypeId, String dictValue) {
        return baseDao.selectByDictTypeId(dictTypeId, dictValue);
    }

    /**
     * 根据字典类型查询字典数据列表
     *
     * @param type
     * @return
     */
    @Override
    public List<SysDictDataDTO> findListByDictType(String type) {
        List<SysDictDataEntity> entities = baseDao.findListByDictType(type);
        if (entities == null || entities.size() <= 0) {
            return null;
        }
        return ConvertUtils.sourceToTarget(entities, SysDictDataDTO.class);
    }
}